/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.component;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellEditor;
import net.worlds.Main;
import net.worlds.Util;


public class JColorPicker extends JComponent{
    private Color value;
    private static JColorChooser chooser;
    private static ChangeListener listener = null;
    public JColorPicker(final TableCellEditor ed, Color c)
    {
        value = c;
        this.addMouseListener(new MouseListener()
        {

            public void mouseClicked(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {
                JFrame picker = new JFrame("Select Color");
                try
                {
                    picker.setIconImages(Util.icons);
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
                chooser.setColor(value);
                picker.addWindowListener(new WindowListener()
                {

                    public void windowOpened(WindowEvent e) {

                    }

                    public void windowClosing(WindowEvent e) {
                        ed.stopCellEditing();
                    }

                    public void windowClosed(WindowEvent e) {

                    }

                    public void windowIconified(WindowEvent e) {

                    }

                    public void windowDeiconified(WindowEvent e) {

                    }

                    public void windowActivated(WindowEvent e) {

                    }

                    public void windowDeactivated(WindowEvent e) {

                    }
                });
                if(listener != null)
                    chooser.getSelectionModel().removeChangeListener(listener);
                listener = new ChangeListener()
                {

                    public void stateChanged(ChangeEvent e) {
                        value = chooser.getColor();
                    }

                };
                chooser.getSelectionModel().addChangeListener(listener);
                picker.add(chooser);
                picker.pack();
                picker.setResizable(true);
                picker.setLocationRelativeTo(Main.getApplet());
                picker.setVisible(true);
            }
 
            public void mouseExited(MouseEvent e) {

            }

        });
    }
    public Color getValue()
    {
        return value;
    }
    @Override
    public void paintComponent(Graphics g)
    {
        g.setColor(value);
        g.fillRect(2, 2, 10, getHeight() - 4);
        g.setColor(java.awt.Color.black);
        g.drawString(Util.colorToString(value), 15, getHeight() - 2);
    }
    static
    {
        chooser = new JColorChooser(java.awt.Color.white);
        chooser.setPreviewPanel(new JPanel());
    }
}
